﻿namespace UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using Common.Localization;

    public partial class CellContainer : UserControl, ILocalizable
    {
        private int rowsCount, colsCount;

        private List<Cell> Cells;
        
        public CellContainer()
        {
            this.rowsCount = 0;
            this.colsCount = 0;
            this.Cells = new List<Cell>();
            this.HScroll = false;

            this.InitializeComponent();
        }

        public event EventHandler CellOptionsClicked;
        
        public void AddCell(Cell cell)
        {
            if (this.Cells.Count == 0)
            {
                // додаєм першу комірку
                cell.Top = Cell.Margin;
                cell.Left = Cell.Margin;
                this.colsCount++;
                this.rowsCount++;
            }
            else
            {
                cell.Left = this.Cells.Last().Right + 2 * Cell.Margin;
                cell.Top = this.Cells.Last().Top;
                if(this.rowsCount == 1)
                {
                    this.colsCount++;
                }

                // Комірка висувається за межі контейнера - переносим в наступний рядок.
                if (cell.Right > this.Width)
                {
                    cell.Top = this.Cells.Last().Bottom + 2 * Cell.Margin;
                    cell.Left = Cell.Margin;
                    this.rowsCount++;
                }
            }

            this.Cells.Add(cell);
            this.Controls.Add(cell);
            cell.OptionsClicked += this.CellOptionsClickedEventHandler;
        }

        public void RemoveCell(Cell cell)
        {
            this.Cells.Remove(cell);
            this.Controls.Remove(cell);
            this.AllocateCells();
        }

        public void Clear()
        {
            for (int i = 0; i < this.Cells.Count; i++)
            {
                this.Controls.Remove(this.Cells[i]);
            }

            this.Cells.Clear();
        }

        public void Localize()
        {
            foreach (var control in this.Controls)
            {
                try
                {
                    (control as ILocalizable).Localize();
                }
                catch (Exception)
                {
                }
            }
        }

        public void RefreshChanelValues(DateTime currentTime)
        {
            foreach (var cell in this.Cells)
            {
                cell.SetChannelValue(currentTime);
            }
        }

        private void AllocateCells()
        {
            for (int i = 0; i < this.Cells.Count; i++)
            {
                Cell currentCell = this.Cells[i];
                int index = this.Controls.IndexOf(currentCell);
                if (index != -1)
                {
                    if (i == 0)
                    {
                        this.Controls[index].Left = Cell.Margin;
                        this.Controls[index].Top = Cell.Margin;
                    }
                    else
                    {
                        int expectedRight = this.Cells[i - 1].Right + 2 * Cell.Margin + Cell.Width;
                        if (expectedRight > this.Width)
                        {
                            this.Controls[index].Left = Cell.Margin;
                            this.Controls[index].Top = this.Cells[i - 1].Bottom + 2 * Cell.Margin;
                        }
                        else
                        {
                            this.Controls[index].Left = this.Cells[i - 1].Right + 2 * Cell.Margin;
                            this.Controls[index].Top = this.Cells[i - 1].Top;
                        }
                    }
                }
            }
        }

        private void CellContainerResizeEventHandler(object sender, EventArgs e)
        {
            int newColsCount = this.Width / (Cell.Width + 2 * Cell.Margin);
            if (this.HorizontalScroll.Visible)
            {
                int w = this.Width;
            }

            if (newColsCount != this.colsCount)
            {
                if (this.Cells.Count > this.colsCount)
                {
                    try
                    {
                        List<Cell> c = this.Cells.ToList();
                        foreach (var cell in this.Cells)
                        {
                            this.Controls.Remove(cell);
                        }

                        this.Cells.Clear();
                        foreach (var cell in c)
                        {
                            this.AddCell(cell);
                        }

                        this.colsCount = newColsCount;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        private void CellContainerLoad(object sender, EventArgs e)
        {
            this.colsCount = this.Width / (Cell.Width + 2 * Cell.Margin);
        }

        private void CellOptionsClickedEventHandler(object sender, EventArgs e)
        {
            if(this.CellOptionsClicked != null)
            {
                this.CellOptionsClicked(sender, null);
            }
        }
    }
}